package org.xqp.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xqp.basic.exception.MyException;
import org.xqp.basic.util.JsonResult;
import org.xqp.order.dto.SaveOrderDto;
import org.xqp.order.service.IOrderPetService;

/**
 *  这是订单相关的Controller
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderPetService orderPetService;

    //接收前台传过来的订单并保存到数据库
    @PostMapping("/save")
    public JsonResult placeOrder(@RequestBody SaveOrderDto dto){
        try {
            String orderNo = orderPetService.placeOrder(dto);
            return JsonResult.me().setResultObj(orderNo);
        } catch (MyException e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("程序异常，正在殴打程序员");
        }
    }
}
